package com.gmrz.util;

import org.apache.commons.lang3.StringEscapeUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public class Strings {
    public static final String LINE_SEPARATOR = System.getProperty("line.separator");
    public static final String WHITE_SPACES = " \t\r\n";
    public static final char SPACE = ' ';

    public static char[] charArray(int length, char value) {
        char[] chars = new char[length];

        Arrays.fill(chars, value);

        return chars;
    }

    public static boolean isNullOrEmpty(String str) {
        return (str == null) || (str.trim().isEmpty());
    }

    public static boolean isNotBlank(String str) {
        if (null != str && !str.trim().isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean equals(String s1, String s2) {
        if (s1 == null) {
            return s2 == null;
        }
        return s1.equals(s2);
    }

    public static boolean equalsIgnoreCase(String s1, String s2) {
        if (s1 == null) {
            return s2 == null;
        }
        return s1.equalsIgnoreCase(s2);
    }

    public static boolean containsOnly(String str, String chars) {
        for (int i = 0; i < str.length(); i++) {
            if (chars.indexOf(str.charAt(i)) < 0) {
                return false;
            }
        }
        return true;
    }

    public static String padLeft(String str, int totalLength, char padding) {
        int padLength = totalLength - str.length();
        if (padLength > 0) {
            str = new String(charArray(padLength, padding)) + str;
        }

        return str;
    }

    public static String padLeft(String str, int totalLength) {
        return padLeft(str, totalLength, ' ');
    }

    public static String padRight(String str, int totalLength, char padding) {
        int padLength = totalLength - str.length();
        if (padLength > 0) {
            str = str + new String(charArray(padLength, padding));
        }

        return str;
    }

    public static String padRight(String str, int totalLength) {
        return padLeft(str, totalLength, ' ');
    }

    public static String[] split(String str, String separator, int limit, boolean trimParts) {
        String[] parts = str.split(separator, limit);

        if (trimParts) {
            trimAll(parts);
        }
        return parts;
    }

    public static void trimAll(String[] strings) {
        for (int i = 0; i < strings.length; i++) {
            if (!isNullOrEmpty(strings[i]))
                strings[i] = strings[i].trim();
        }
    }

    public static String escapeHtmlAndScript(String value) {
        if (null != value) {
            value = StringEscapeUtils.escapeHtml3(value);
            value = StringEscapeUtils.escapeHtml4(value);
            value = StringEscapeUtils.escapeEcmaScript(value);
        }
        return value;
    }

    public static String listToString(List<String> list) {
        if (null == list) {
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            stringBuffer.append(list.get(i));
            if (i != list.size() - 1) {
                stringBuffer.append(",");
            }
        }
        return stringBuffer.toString();
    }

    public static Date strToDate(String date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1;
    }

    public static boolean isBase64url(String str) {
        return str.matches("^[A-Za-z0-9_-]+$");
    }


    public static String codeConversion(int code){
        if (code == 0){
            return "";
        }
        if (code == 1){
            return "SE";
        }
        if (code == 2){
            return "TEE";
        }
        if (code == 3){
            return "KEYSTORE";
        }
        return "";
    }

}
